package cn.jdx.config;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class UserRealm extends AuthorizingRealm {
    @Override
    //(1)授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行了授权");

        return null;
    }

    @Override
    //(2)认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行了认证");
        //数据库获取数据
        String name = "root";
        String pwd = "123";
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        //用户名认证
        if(!token.getUsername().equals(name)){
            return null;//抛出用户名不一致的异常
        }
        //密码认证
        return new SimpleAuthenticationInfo("",pwd,"");
    }
}
